<?php
namespace Model;
use DB;

class Account extends \Model {
	public static function get_account($user_id = 0) {
		$query = DB::select()->from('accounts');
		if ($user_id != 0) {
			$query->where('user_id', '=', $user_id);      
		}
		$r = $query->execute();
		return $r->as_array();
	} 

	public static function add($user_message) {
		$data = $user_message;
		$query = DB::select()->from('accounts');
		$query->where('idstr', '=', $data['idstr']);      
		$r = $query->execute();
		if (count($r->as_array()) > 0) return true;
		DB::insert('accounts')->set($data)->execute();
	}


	public static function update($user_message) {
		$data = $user_message;
		$query = DB::update('accounts');
		$query->where('idstr', '=', $data['idstr']);      
		$query->set($data)->execute();
	}

	public static function get_access_token($id = 0) {
		$query = DB::select('access_token')->from('accounts');
		if ($id == 0) {
			$account = self::get_account();
			if (count($account) == 0) return '';
			$acc = rand(0, count($account)-1);
			$id = $account[$acc]['id'];
		}
		$query->where('id', '=', $id);      
		$r = $query->execute();
		$r = $r->as_array();
		return $r[0]['access_token'];
	}
	public static function get_access_token_by_screen_name($screen_name = 'irobot2011') {
		$query = DB::select('access_token')->from('accounts');
		$query->where('screen_name', '=', $screen_name);      
		$r = $query->execute();
		$r = $r->as_array();
		return $r[0]['access_token'];
	}

	public static function get_profile_image_by_screen_name($screen_name = 'irobot2011') {
		$query = DB::select('profile_image_url')->from('accounts');
		$query->where('screen_name', '=', $screen_name);      
		$r = $query->execute();
		$r = $r->as_array();
		return $r[0]['profile_image_url'];
	}

}
